<template>
<section>
  <el-row>
    <el-col :span="6">
      <el-input placeholder="请输入内容" v-model="searchKey" prefix-icon="el-icon-fa-search">
        <div slot="append">
          <el-button type="primary" icon="el-icon-fa-search" @click=" pager.pager.pageNumber = 1 ;doSearch()"></el-button>
        </div>
      </el-input>
    </el-col>
    <el-col :span="6" :offset="12">
      <el-button type="primary" icon="el-icon-fa-plus" @click="addProvenance" size="small">添加产区</el-button>
    </el-col>
  </el-row>
  <el-table :data="pager.dataList" border stripe style="width: 100%">
    <el-table-column prop="id" label="ID" header-align="center" align="center" width="55">
    </el-table-column>
    <el-table-column prop="flag" label="生豆原产国" show-overflow-tooltip header-align="center" align="center">
        <template slot-scope="scope">
                  {{scope.row.flag.name}} {{scope.row.flag.icon}}
        </template>
    </el-table-column>
      <el-table-column prop="name" label="产区" show-overflow-tooltip header-align="center" align="center">
      </el-table-column>
      <el-table-column prop="height" label="海拔"  show-overflow-tooltip header-align="center" align="center">
      </el-table-column>
      <el-table-column prop="rainfall" label="年降水量" show-overflow-tooltip header-align="center" align="center">
      </el-table-column>
      <el-table-column prop="temperature" label="年平均气温" show-overflow-tooltip header-align="center" align="center">
      </el-table-column>
      <el-table-column label="操作" show-overflow-tooltip header-align="center" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-button title="编辑产区" size="mini" type="primary" icon="el-icon-fa-edit" @click="handleEdit(scope.$index,scope.row)"></el-button>
            <el-button title="删除产区" size="mini" type="primary" icon="el-icon-fa-trash" @click="handleDelete(scope.$index,scope.row)"></el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
        <el-row>
            <el-col :span="6" :offset="18">
                <el-pagination background small style="float:right" layout="prev, pager, next"
                               :total="pager.pager.recordCount" :page-size="pager.pager.pageSize"
                               :current-page.sync="pager.pager.pageNumber" v-show="pager.pager.pageCount != 0"
                               @current-change="changePage">
                </el-pagination>
            </el-col>
        </el-row>

         <!-- 弹框区域-->
        <el-dialog :title="provenance.id == 0 ? '添加产区' : '编辑产区' " :visible.sync="addEditShow" width="50%">
            <el-form :model="provenance" :rules="$rules.area" ref="provenanceForm">
                <el-form-item label="原产国" :label-width="formLabelWidth" prop="flag">
                    <el-select v-model="provenance.flag" filterable placeholder="请选择生豆原产国">
                      <el-option
                        v-for="item in countries"
                        :key="item.name"
                        :label="item.name + '  ' +item.icon"
                        :value="item.orgin">
                      </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="产区名称" :label-width="formLabelWidth" prop="name">
                    <el-input v-model="provenance.name" auto-complete="off" suffix-icon="el-icon-fa-vcard"
                              placeholder="请填写产区名称"></el-input>
                </el-form-item>
                <el-form-item label="海拔" :label-width="formLabelWidth" prop="height">
                    <el-input  v-model="provenance.height" auto-complete="off" suffix-icon="el-icon-fa-text-height"
                              placeholder="请填写产区海拔">
                              <template slot="append">m</template>
                              </el-input>
                </el-form-item>
                <el-form-item label="土壤深度" :label-width="formLabelWidth" prop="depth">
                      <el-input v-model="provenance.depth"  auto-complete="off"  placeholder="请填写土壤深度">
                        <template slot="append">m</template>
                      </el-input>
                </el-form-item>
                <el-form-item label="年降水量" :label-width="formLabelWidth" prop="rainfall">
                    <el-input v-model="provenance.rainfall" auto-complete="off" suffix-icon="el-icon-fa-shower"
                              placeholder="请填写平均年降水量">
                              <template slot="append">mm</template>
                              </el-input>
                </el-form-item>
                <el-form-item label="平均气温" :label-width="formLabelWidth" prop="temperature">
                    <el-input v-model="provenance.temperature" auto-complete="off" suffix-icon="el-icon-fa-thermometer-full"
                              placeholder="请填写年平均气温">
                              <template slot="append">℃</template>
                              </el-input>
                </el-form-item>

                <el-form-item label="水源" :label-width="formLabelWidth" prop="waterInfo">
                    <el-input v-model="provenance.waterInfo" auto-complete="off" suffix-icon="el-icon-fa-trello"
                              placeholder="请填写水源信息"></el-input>
                </el-form-item>

                <el-form-item label="产区详情" :label-width="formLabelWidth" prop="description">
                    <editor id="editor_id" height="500px" width="700px"
                      :content="provenance.description"
                      :extraFileUploadParams="params"
                      :uploadJson="imgupload"
                      :afterChange="afterChange()"
                      pluginsPath="/static/kindeditor/plugins/"
                      :loadStyleMode="false"
                      @on-content-change="onContentChange"></editor>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addEditShow = false ; provenance = {id:0,flag:'UNSELECTED'}">取 消</el-button>
                <el-button type="primary" @click="saveOrUpdateprovenance('provenanceForm')">确 定</el-button>
            </div>
        </el-dialog>
  </section>
</template>

<style>
</style>

<script>
import config from "../../common/config";
export default {
  data() {
    return {
      searchKey: "",
      content: "",
      params: {
        base: config.http.baseUrl
      },
      imgupload: config.http.baseUrl + "image/kind/upload",
      pager: {
        pager: {
          pageCount: 1,
          pageNumber: 1,
          pageSize: 15,
          recordCount: 1
        },
        dataList: []
      },
      countries: [],
      selected: [],
      addEditShow: false,
      resetShow: false,
      grantShow: false,
      type: "role",
      provenance: {
        depth: 0,
        description: "",
        flag: null,
        height: 0,
        id: 0,
        name: "",
        rainfall: 0,
        soilInfo: "",
        temperature: 0,
        waterInfo: ""
      },
      formLabelWidth: "100px"
    };
  },
  methods: {
    saveOrUpdateprovenance(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var callback = result => {
            this.changePage();
            this.addEditShow = false;
          };
          this.provenance.id
            ? this.$api.Area.update(this.provenance, callback)
            : this.$api.Area.save(this.provenance, callback);
        } else {
          return false;
        }
      });
    },
    changePage() {
      if (this.searchKey) {
        this.doSearch();
      } else {
        this.loadData();
      }
    },
    handleEdit(index, row) {
      this.provenance = JSON.parse(JSON.stringify(row));
      this.provenance.flag = this.provenance.flag.code;
      this.addEditShow = true;
    },
    handleDelete(index, row) {
      this.$confirm("确认删除产区?", "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$api.Area.delete(row.id, result => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          window.setTimeout(() => {
            if (this.searchKey) {
              this.doSearch();
            } else {
              this.loadData();
            }
          }, 2000);
        });
      });
    },
    doSearch() {
      this.$api.Area.search(
        this.pager.pager.pageNumber,
        this.searchKey,
        result => {
          this.pager = result.pager;
        }
      );
    },
    loadData() {
      this.$api.Area.list(this.pager.pager.pageNumber, result => {
        this.pager = result.pager;
      });
    },
    onContentChange(val) {
      this.provenance.description = val;
    },
    afterChange() {},
    addProvenance() {
      this.addEditShow = true;
      this.provenance = {
        id: 0,
        flag: "UNSELECTED"
      };
    }
  },
  created() {},
  mounted() {
    this.$api.Area.countries(result => {
      this.countries = result.countries;
      this.loadData();
    });
  }
};
</script>
